package org.jground.rx.ui.zk;

import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;
import org.jground.rx.RXAccess;
import org.jground.rx.RXSystem;
import org.jground.rx.IRXObject;
import org.jground.zk.zulX.HboxX;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;

public class UIWindowViewCommandPanel extends HboxX implements EventListener {

    private static Log logger = LogFactory.getLog(UIWindowViewCommandPanel.class);
    // 
    private Button approveBt;
    private Button approveAndCloseBt;
    private Button closeBt;
    // private Button homeBt;
    private Button editBt;
    //
    private UIFrame uiFrame;
    private UIViewWindow viewWindow;
    //
    private IRXObject rxd;

    //
    public UIWindowViewCommandPanel(UIFrame uiFrame, UIViewWindow viewWindow) {
        this.uiFrame = uiFrame;
        this.viewWindow = viewWindow;
        this.setSclass(UIStyles.BUTTONGROUP);
        //
        editBt = new Button("Edit");
        approveBt = new Button("Approve");
        approveAndCloseBt = new Button("Approve and close");
        closeBt = new Button("Close");
        // homeBt = new Button("Home");
        //

        //
        editBt.addEventListener(Events.ON_CLICK, this);
        approveBt.addEventListener(Events.ON_CLICK, this);
        approveAndCloseBt.addEventListener(Events.ON_CLICK, this);
        closeBt.addEventListener(Events.ON_CLICK, this);
    }

    public void onEvent(Event event) {
        if (event.getTarget() == approveBt) {
            rxd.approve();
            // rxDataObject.Save();
            viewWindow.setData(rxd);
        }
        if (event.getTarget() == closeBt || event.getName().equals(Events.ON_CLOSE)) {
            viewWindow.close();
        }
        // if (event.getTarget() == homeBt) {
        // uiFrame.showHomePanel();
        // }
        if (event.getTarget() == editBt) {
            logger.info("checkout user : " + rxd.getCheckedOutUser() + " "
                    + RXSystem.getRXPrincipalMethods().getUserName());
            if (rxd.checkOut()) {
                UIEditWindow editWindow = uiFrame.getEditWindow(rxd.getMoid());
                editWindow.setData(rxd);
                uiFrame.setMainPanel(editWindow, false);
            } else {
                UIMessageboxUtils.showCheckOutFailed(uiFrame.getUISynopsisRendererFactory().toString(rxd), rxd
                        .getCheckedOutUser());
            }
        }
    }

    public void setData(IRXObject mod) {
        this.rxd = mod;
        removeAll();
        if (RXAccess.getInstance().canWrite(mod)) {
            logger.info("Can check out " + mod.getOidString());
            add(editBt);
            if (RXAccess.getInstance().canApprove(mod)) {
                add(approveBt);
            }
        }
        add(closeBt);
        // add(homeBt);
    }

}